package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwalT;
import cn.e4j.modular.crm.service.GwalTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 客户联系人资料管理
 * Created by miaoyl on 2019-1-16.
 */
@Controller
@RequestMapping("/crm/gwai301")
public class Gwai301Controller extends BaseController {

    @Autowired
    private GwalTService gwalTService;

    /**
     * 跳转客户联系人资料管理页面
     */
    @RequiresPermissions("crm:gwai301:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai301.html";
    }

    /**
     * 跳转到查看编辑的页面
     */
    @RequestMapping("/gwai301_add")
    public String addView() {
        return "/pc/crm/gwai301_edit.html";
    }

    /**
     * 查询客户联系人信息列表
     */
    @RequestMapping("/getGwallist")
    @RequiresPermissions("crm:gwai301:init")
    @ResponseBody
    public ResponseData getGwallist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent",shiroUser.getDBC());
        inDto.put("lang",LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwalTService.getGwallist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai301:init")
    @ResponseBody
    public ResponseData add(GwalT gwalT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断是否常用地址
        GwalT theGwalT = gwalTService.getByGwal003(shiroUser.getDBC(),gwalT.getGWAL001());
        if (theGwalT != null && gwalT.getGWAL003().equals("Y")) {
            //throw new ServiceException(CrmExceptionEnum.EXISTED_THE_CODE);
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWAL.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWAL.getMessage());
        }
        gwalTService.add(gwalT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai301:init")
    @ResponseBody
    public ResponseData edit(GwalT gwalT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断是否常用地址
        GwalT theGwalT = gwalTService.getByGwal003(shiroUser.getDBC(),gwalT.getGWAL001());
        if(!(theGwalT != null && gwalT.getGWAL002().equals(theGwalT.getGWAL002()))) {
            if (gwalT.getGWAL003().equals("Y")) {
                //throw new ServiceException(CrmExceptionEnum.EXISTED_THE_CODE);
                return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWAL.getCode(),
                        CrmExceptionEnum.EXISTED_THE_GWAL.getMessage());
            }
        }
        gwalTService.edit(gwalT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai301:init")
    @ResponseBody
    public ResponseData del(GwalT gwalT) {
        gwalTService.delete(gwalT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwai301:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwalT> gwalTList = inDto.getReqList(GwalT.class);
        gwalTService.delDataBatch(gwalTList);
        return SUCCESS_TIP;
    }

}
